<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="url" value="<%=request.getContextPath()%>"></c:set>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${url }/js/jquery-3.1.1.min.js"></script>
</head>
<body>

	<ul>
		<c:forEach items="${z_tree }" var="province">
			<li id="${province.code }" onclick="city(this)">${province.name }
			</li>
		</c:forEach>
	</ul>
</body>
<script type="text/javascript">
   function city(obj){
	   var id=obj.id;
	   urlhttp="<%=request.getContextPath()%>/tree/CityJson.action";
		$.ajax({
			type : 'post',
			data : {
				'code' : id
			},
			url : urlhttp,
			dataType : 'json',
			success : function(data) {
				var citys= data;
				if($("#" + id).next("ul").length==0){
					$("#" + id).after("<ul id=li"+id+"  ></ul>");
					if (citys != null) {
						for (var i = 0; i <= citys.length; i++) {
							$("#li"+id).append(
									"<li id="+citys[i].code+" onclick=area(this)>"
									+ citys[i].name + "</li>")
						}
					}
				}else{
					$("#" + id).next("ul").remove()
				}
				
				
					
			}
			
			
		});
   }
 
		function area(obj){
			   var id=obj.id;
			   urlhttp="<%=request.getContextPath()%>/tree/AreaJson.action";
				$.ajax({
					type : 'post',
					data : {
						'code' : id
					},
					url : urlhttp,
					dataType : 'json',
					success : function(areas) {
						if($("#" + id).next("ul").length==0){
						$("#" + id).after("<ul id=li"+id+"  ></ul>");
						if (areas != null) {
							for (var i = 0; i <= areas.length; i++) {
								$("#li"+id).append(
										"<li id="+areas[i].code+" onclick=area(this) >"
										+ areas[i].name + "</li>")
							}
						}
						}else{
							$("#" + id).next("ul").remove()
						}
					}
				});
		}
		
		/*  if($("#" + id ).children().length>0){
		   
		   $("#" + id ).children().remove();
		   alert("执行了删除节点")
		 }else{ 
		   
		    alert("执行了增加节点")
		 } */
	
</script>
</html>